SHELL=cmd.exe
# make 参数
package_name=name.hankc.spring
main=
project=spring-basic

# 环境变量
# tomcat 启动
SHELL_CATALINA_HOME=$CATALINA_HOME

catalina_home=$(shell echo $(value SHELL_CATALINA_HOME))
ifeq ($(wildcard $(catalina_home)), )
catalina_home=E:\apache-tomcat-8.0.20
export CATALINA_HOME=$(catalina_home)
endif

run_command=$(catalina_home)\bin\catalina.bat run

output_dir=./target

# 可执行文件
maven_bin=E:\apache-maven-3.6.3\bin\mvn
java_bin=E:\Java\jdk-11\bin\java

EMPTY=
SPACE= $(EMPTY) # 空格
# LIBS=$(subst $(SPACE),:,$(wildcard $(target)/lib/*.jar))
# const_cls_path=$(target)/classes:$(LIBS)

########################################
# 命令定义
########################################
all: application

test:
	@echo $(main)

package: clean
	$(maven_bin) package

copy: package
	copy $(output_dir)\$(project).war $(catalina_home)\webapps

run: copy
	$(run_command)

compile: clean
	$(maven_bin) compile

install:
	$(maven_bin) install

dependency: compile
	$(maven_bin) dependency:copy-dependencies

before_java:
ifeq ($(wildcard LIBS), )
LIBS=$(subst $(SPACE),;,$(wildcard $(output_dir)/lib/*.jar))
const_cls_path=$(output_dir)/classes;$(LIBS)
endif

application: dependency before_java java

java:
	$(java_bin) -cp $(const_cls_path) $(package_name).$(main)

.PHONY : clean_tomcat
tomcat_app:=$(catalina_home)\webapps\$(project)
clean_tomcat:
ifneq ($(wildcard $(tomcat_app).war), )
	del /S/Q $(tomcat_app).war
endif
ifneq ($(wildcard $(tomcat_app)), )
	rmdir /Q/S $(tomcat_app)
endif

.PHONY : clean
clean: clean_tomcat
	$(maven_bin) clean